﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class CustomerOrderViewerCtrl : BaseEditControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCompany.Text = Branches.CurrentBranch.s_Name;
                lblAddress.Text = Branches.CurrentBranch.s_Address;
                lblABN.Text = Branches.CurrentBranch.s_ABN;
                lblFax.Text = Branches.CurrentBranch.s_Fax;
                lblPhone.Text = Branches.CurrentBranch.s_Phone;
                litFooter.Text = SysVars.InvoiceFooter;
                
            }
        }

        protected void btnLoad_OnClick(object sender, EventArgs e)
        {
            if (hidID.Value != "")
                LoadCustomerOrderByID(int.Parse(hidID.Value));
        }

        public void LoadCustomerOrderByID(int invoiceID)
        {
            View_CustomerOrders ivc = View_CustomerOrders.GetCustomerOrderByID(invoiceID);
            LoadCustomerOrder(ivc);
        }

        private void LoadCustomerOrder(View_CustomerOrders order)
        {
            if (order.RowCount <= 0)
                return;

            litNote.Text = "<b>Note:</b>" + order.s_Note;
            if (order.s_CreatedDate != "")
                lblDate.Text = order.CreatedDate.ToString(SysVars.DateFormat);

            lblInvoiceNo.Text = order.OrderNo;
            divInvoiceNo.InnerHtml = Utilities.GenerateBarCode(order.OrderNo);

            View_OrderItems items = View_OrderItems.GetOrderItems(order.ID);
            orderDetails.SourceObject = items;
            orderDetails.UpdateTotalAmt();
            orderDetails.Refresh();

            decimal gst = 0;
            for (int i = 0; i < items.RowCount; i++)
            {
                gst += items.GST;
                items.MoveNext();
            }

            lblTotalAmt.Text = string.Format("{0:C}", order.TotalAmount);
            lblTotalGST.Text = string.Format("{0:C}", gst);
            if (order.s_Discount != "")
            {
                if(order.s_DiscountAmount != "")
                    lblSpecialDiscount.Text = string.Format("{0:C}", order.DiscountAmount);
                else
                    lblSpecialDiscount.Text = string.Format("{0}%", order.DiscountPercent);
                lblSpecialDiscount.Visible = true;
                lblSpecialDiscountLabel.Visible = true;
            }
            lblCustomer.Text = order.IsColumnNull(View_Invoices.ColumnNames.CustomerID) ? order.s_CustomerName : order.s_CustomerName;
            if (order.s_ShipAddress != "")
                lblCustomer.Text += " - " + order.s_ShipAddress;
        }
    }
}